package completable;

import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

/**
 * @author huchengchao <huchengchao@kuaishou.com>
 * Created on 2022-03-17
 */
public class CompletableFutureTest {

    @Test
    public void runAfterBothTest() {
        //第一个异步任务，常量任务
        CompletableFuture<String> first = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                System.out.println("HelloWorld:  " + Thread.currentThread().getName());
                return "hello world";
            }
        });
        ExecutorService executor = Executors.newSingleThreadExecutor();
        CompletableFuture<Void> future = CompletableFuture
                //第二个异步任务
                .supplyAsync(new Supplier<String>() {
                    @Override
                    public String get() {
                        System.out.println("HelloJava:  " + Thread.currentThread().getName());
                        return "hello java";
                    }
                })
                // () -> System.out.println("OK") 是第三个任务
                .runAfterBothAsync(first, new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("OK:  " + Thread.currentThread().getName());
                        System.out.println("OK");
                    }
                });
        System.out.println("hahaha");
        future.join();
    }

    @Test
    public void whenCompleteTest() {
        CompletableFuture<String> first = CompletableFuture.supplyAsync(() -> {
            System.out.println("HelloWorld:  " + Thread.currentThread().getName());
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello world";
        }, Executors.newFixedThreadPool(5));

        first.whenComplete((s, throwable) -> {
            System.out.println("whenComplete: " + Thread.currentThread().getName());
            System.out.println("whenComplete");
        });
        System.out.println("End");
        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void cancelTest() {
        CompletableFuture<String> first = CompletableFuture.supplyAsync(() -> {
            System.out.println("HelloWorld:  " + Thread.currentThread().getName());
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello world");
            return "hello world";
        }, Executors.newFixedThreadPool(5));
        first.whenComplete((s, throwable) -> {
            System.out.println(throwable);
            System.out.println("whenComplete: " + Thread.currentThread().getName());
            System.out.println("whenComplete");
        });
        first.cancel(true);

        try {
            Thread.sleep(10000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
